#!/bin/bash

if [[ $(id -u) != "0" ]]; then
    printf "\e[42m\e[31mError: You must be root to run this install script.\e[0m\n"
    exit 1
fi


function changssh {
#更改ssh端口
 sed -i 's/Port 22/Port 8799/g'  /etc/ssh/sshd_config
#启用ssh key登录，
  mkdir /root/.ssh
 echo "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAiH6uaXQA0K54of5TdpGP9v2z9CaXqnnwyNoCMwkRYPH3X7CJk61HunR59zgDoAbT/+eJiCbSEN5/27gGd1kysct/4PDzJ/JpJI3OhKk2185LRoa/BXZaGfEUz4r53M01tGTMyom9VGjFroTUHeBkj2BfAzIo+SFp+ij1RRzM75ZN/y84rGUvGR8+tM3+PbE+6W0mvY6EdKD0YY0bGtcO9xMaFB7sfZk/fUxQSNnrkfYNKA5rWIlLC3JNXwp0M77dK2pSvU3mGodC6UvWv2GU4Q++tj577M9QTv2bI6Lt7nwQHORcAZ5oknNjTKfO8cyvzN8jjCUKzj5zLave6YJB2Q== rsa-key-20150202" >> /root/.ssh/authorized_keys
	
	 sed -i 's/#AuthorizedKeysFile/AuthorizedKeysFile/g' /etc/ssh/sshd_config
	#禁用密码登录
	 sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
  }

  
  
 

#功能7 删除local-lvm，空间合并到 local
function delLocalLVM {
    lvremove pve/data
	vgdisplay pve  #查看PVE的状态
	lvextend -l +100%FREE -r pve/root
	df -h #查看更改后的情况
	 #开始选择安装
	doselect
  }


  

#功能6 安装nfs 服务端，
function nfsserver {
	apt-get update
    apt-get install nfs-common nfs-kernel-server  -y
	mkdir /nfs
    chmod 777 -R /nfs
    echo "/nfs    *(rw,sync,no_root_squash,no_subtree_check,insecure) " >> /etc/exports
    /etc/init.d/nfs-kernel-server restart
    echo "目录是 /nfs"
	printf "
####################################################
#                                                  #
#nfs安装完毕，目录为/nfs      
#                                                  #
#                                                  #
####################################################
"
#开始选择安装
	doselect
}

#功能5 安装mysql数据库和php myadmin
function installmysql {
   apt-get install mysql-server mysql-client -y
   #apt-get install phpmyadmin -y
   #解除绑定，允许远程访问
   sed -i 's/bind-address/#bind-address/g' /etc/mysql/my.cnf
   #echo "Please input root password to GRANT ALL PRIVILEGES"
   # read sqlPASSWORD
   #赋予root用户远程权限
    #mysql -u root -p“${sqlPASSWORD}” -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${sqlPASSWORD}' WITH GRANT OPTION;"
    #mysql -u root -p“${sqlPASSWORD}” -e "FLUSH PRIVILEGES;"
    #/etc/init.d/mysql restart
doselect
}
#功能4安装node-red
function installnodered {
    apt-get install curl -y
	curl -sL https://deb.nodesource.com/setup_16.x |  bash -
	apt-get install nodejs   -y
	npm install -g cnpm --registry=https://registry.npm.taobao.org
    cnpm install -g --unsafe-perm node-red
	npm install -g --unsafe-perm node-red
	
	cnpm install -g pm2
	npm install -g pm2
	
	#增加登录验证
	mypath="/root/.node-red/settings.js"
	
	 echo "adminAuth: {" >> $mypath
	 echo "type: "credentials"," >> $mypath
	  echo "     users: [{" >> $mypath
	   echo "username: "admin"," >> $mypath
	    echo "password: "$2a$08$fku199hVGY4fxiM.6w9M0.7ynwRInqCSswgJ8ffcFSxxTaRRzV2k."," >> $mypath
		 echo "permissions: "*"" >> $mypath
		  echo "}]" >> $mypath
		   echo " }," >> $mypath
	  
    pm2 start node-red
	pm2 save
	pm2 startup
	
	doselect
  }
#功能3安装docker
function installdocker {
curl -sSL https://get.daocloud.io/docker | sh #国内一键安装脚本
rm -rf /etc/docker/daemon.json
mypath="/etc/docker/daemon.json"
 echo "{" >> $mypath
 echo ""registry-mirrors" : [" >> $mypath
 echo ""http://jh0nsih8.mirror.aliyuncs.com"," >> $mypath
 echo ""http://registry.docker-cn.com"," >> $mypath
 echo ""http://docker.mirrors.ustc.edu.cn"," >> $mypath
 echo ""http://hub-mirror.c.163.com"" >> $mypath
 echo "]," >> $mypath
 echo ""insecure-registries" : [" >> $mypath
 echo ""registry.docker-cn.com"," >> $mypath
 echo ""docker.mirrors.ustc.edu.cn"" >> $mypath
 echo "]," >> $mypath
 echo ""debug" : true," >> $mypath
 echo ""experimental" : true" >> $mypath
 echo "}" >> $mypath

 systemctl daemon-reload
 systemctl restart docker 
 docker volume create portainer_data 
 docker run -d -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
	
	doselect  
  }


#功能2
function installhttp {
	apt-get install apache2 -y
	#Installing PHP5
	apt-get install php5 libapache2-mod-php5 -y
	/etc/init.d/apache2 restart
	#Getting MySQL Support In PHP5
	apt-cache search php5
	apt-get install php5-mysql  php5-ming php5-ps  -y
	apt-get -y install php5-mysqlnd php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
	/etc/init.d/apache2 restart
	apt-get install php-apc -y
	/etc/init.d/apache2 restart
	#修改网站端口
	sed -i 's/VirtualHost *:80/VirtualHost *:8080/g' /etc/apache2/sites-enabled/000-default
	sed -i 's/80/8080/g' /etc/apache2/sites-enabled/000-default.conf
	sed -i 's/Listen 80/Listen 8080/g' /etc/apache2/ports.conf
	#修改网站目录
	sed -i 's/html//g' /etc/apache2/sites-enabled/000-default.conf
	
	#重启服务
	/etc/init.d/apache2 restart
	doselect
}	
#功能1 必备操作，仅操作一次
function musttodo {

apt-get update
apt-get upgrade -y
#3、安装ifupdown2 时间同步
	apt-get install ntpdate ifupdown2 -y
	ntpdate cn.ntp.org.cn
	echo "NTP=cn.ntp.org.cn" >>  /etc/systemd/timesyncd.conf
	echo "FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org" >>  /etc/systemd/timesyncd.conf
	echo "RootDistanceMaxSec=5" >>  /etc/systemd/timesyncd.conf
	echo "PollIntervalMinSec=32" >>  /etc/systemd/timesyncd.conf
	echo "PollIntervalMaxSec=2048" >>  /etc/systemd/timesyncd.conf
	systemctl restart systemd-timesyncd
	systemctl status systemd-timesyncd

doselect	
  }
#选择要进行的操作
function doselect {
echo "输入数字键进行操作选择 "
echo "1. 时间同步"
echo "2. install apahe and php"
echo "3. install docker"
echo "4. install node-red"
echo "5. install mysql and phpmyadmin"
echo "6. 安装 nfs server"
echo "7. 删除local-lvm空间合并到Local"
echo "8. "

read num
case "$num" in
[1] ) (musttodo);;
[2] ) (installhttp);;
[3] ) (installdocker);;
[4] ) (installnodered);;
[5] ) (installmysql);;
[6] ) (nfsserver);;
[7] ) (delLocalLVM);;
[8] ) (delLocalLVM);;
*) echo "OK,Bye!";;
esac
}
#根据系统进行配置
#显示信息
printf "
####################################################################
#
# 脚本在ProxmoxVE6.3.1版本安装包上测试通过。其它版本未测试            #
#                                                                 #
#                                                                 #
###################################################################
"
#开始选择安装

doselect

